package com.situ.javase.day4;

import java.util.Scanner;

import org.junit.Test;

public class ArrayDemo {

	@Test
	public void test1() {
		int score1 = 34;
		int score2 = 67;
		int score3 = 44;
		int score4 = 94;
		int[] array = new int[4];
		// 数组名[下标]在等号左侧表示给指定数组下标位置赋值
		array[0] = 23;
		array[1] = 34;
		array[2] = 45;
		array[3] = 98;
		System.out.println(array[0]);
		System.out.println(array[1]);
		System.out.println(array[2]);
		System.out.println(array[3]);
		// 数组名.length得到数组的长度
		System.out.println(array.length);
		System.out.println(array.length - 1);
		System.out.println("--------------");
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
		System.out.println("-------------");
		for (int i = array.length - 1; i >= 0; i--) {
			System.out.println(array[i]);
		}
	}
	
	@Test
	public void test2() {
		// int类型的默认值0
		// int[]初始化后的数组里面存放的都是int类型的默认值
		int[] array = new int[4];
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
		// double类型的默认值是0.0
		double[] array1 = new double[4];
		int[] array2 = {34, 56, 34, 4};
		int[] array3 = new int[] {34, 56, 34, 4};
	}
	
	// 统计数组里面所有学生的成绩总和
	@Test
	public void test4() {
		int[] array = new int[4];
		array[0] = 23;
		array[1] = 34;
		array[2] = 45;
		array[3] = 98;
		int sum = 0;
		for (int i = 0; i < array.length; i++) {
			sum = sum + array[i];
		}
		System.out.println(sum);
		System.out.println(sum / array.length);
	}
	
	@Test
	public void test5() {
		int[] array = new int[4];
		array[0] = 23;
		array[1] = 34;
		array[2] = 45;
		array[3] = 98;
		for (int i = 0; i <= array.length; i++) {
			System.out.println(array[i]);
		}
	}
	
//	1、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
//	求出上面数组中0-9分别出现的次数
	@Test
	public void test8() {
		int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
		for(int i=0; i<=9; i++) {
			int count = 0;
			for(int j=0; j<scores.length; j++) {
				if(i==scores[j]) {
					count++;
				}
			}
			System.out.println(i+"出现的次数:"+count);
		}
	}

	@Test
	public void test33() {
		int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
		// int[] num = {0,0,0,0,0,0,0,0,0,0};
		// 存放0-9分别出现的次数
		int[] num = new int[10];
		for (int i = 0; i < scores.length; i++) {
			switch (scores[i]) {
			case 0:
				num[0]++;
				break;
			case 1:
				num[1]++;
				break;
			case 2:
				num[2]++;
				break;
			case 3:
				num[3]++;
				break;
			default:
				break;
			}
			
		}
		
		for (int i = 0; i < num.length; i++) {
			System.out.println(i + "的个数" + num[i]);
		}
	}
	
	
//	2、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
//	要求求出其中的奇数个数和偶数个数。
	@Test
	public void test77() {
		int score = 89;
		int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
		int jishuCount = 0;
		int oushuCount = 0;
		for (int i = 0; i < scores.length; i++) {
			if (scores[i] % 2 == 0) {
				oushuCount++;
			} else {
				jishuCount++;
			}
		}
		
		System.out.println("奇数个数：" + jishuCount);
		System.out.println("奇数个数：" + oushuCount);
	}
	
//	3、输入一组学生的成绩，使用数组，然后计算他们的平均值.
	@Test
	public void test6() {
		
	}
	
	@Test
	public void test78() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个字符串");
		String str = scanner.next();// "abcba"
		char[] array = str.toCharArray();
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
	}
	
}
